/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 *//*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package predicates;

import entities.Search;
import interfaces.CompanyAdquiredProfileCapable;
import interfaces.SearchCapable;
import java.text.Normalizer;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import util.filter.Predicate;

/**
 *
 * @author damianfernandez
 * @param <T>
 */
public class CompanyAdquiredProfileCapableSearchCapablePredicate<T extends CompanyAdquiredProfileCapable> implements Predicate<T>, SearchCapable{
    private Search search;
    private String name;
    private Date fromDate;
    private Date toDate;
    private Boolean isRelated;
    
    public CompanyAdquiredProfileCapableSearchCapablePredicate(){
        search=new Search();
    }
    
    public CompanyAdquiredProfileCapableSearchCapablePredicate(Search search){
        this.search=search;
    }
    
    public CompanyAdquiredProfileCapableSearchCapablePredicate(Search search, String name, Date fromDate, Date toDate, Boolean isRelated){
        this.search=search;
        this.name=name;
        this.fromDate=fromDate;
        this.toDate=toDate;
        this.isRelated=isRelated;
    }
    
    @Override
    public boolean test(T o) {
        boolean passSearch=search.test(o.getCandidate());
        boolean passName=true;
        boolean passFromDate=true;
        boolean passToDate=true;
        boolean passIsRelated=true;
        if(name!=null){
            Pattern p = Pattern.compile(Pattern.quote(Normalizer.normalize(name.trim(), Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "")),Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(Normalizer.normalize((o.getCandidate().getName()+" "+o.getCandidate().getLastName()).trim(), Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "")); 
            if(!m.find()){
                passName=false;
            }
        }
        if(fromDate!=null){
            passFromDate=o.getCompanyAdquiredProfile().getAdquisitionDate().compareTo(fromDate)>=0;
        }
        if(toDate!=null){
            passFromDate=o.getCompanyAdquiredProfile().getAdquisitionDate().compareTo(toDate)<=0;
        }
        if(isRelated!=null){
            if(isRelated){
                passIsRelated=o.getCandidate().getUser().getRelatedUser()!=null && o.getCandidate().getUser().equals(o.getCompanyAdquiredProfile().getCompany().getIdUser());
            }
            else{
                passIsRelated=o.getCandidate().getUser().getRelatedUser()==null || !o.getCandidate().getUser().equals(o.getCompanyAdquiredProfile().getCompany().getIdUser());
            }
        }
        return passSearch && passName && passFromDate && passToDate && passIsRelated;
    }

    @Override
    public Search getSearch() {
        return search;
    }
    
}
